package com.fbinggun.fattribute.data;

import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.attribute.IAttribute;
import com.fbinggun.fattribute.status.Modifier;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DataManage {
    private  static  Map<UUID,EntityData> dataMap= new HashMap<>();

    public static void setDataMap(Map<UUID, EntityData> dataMap) {
        DataManage.dataMap = dataMap;
    }
    public static void putEntityData(EntityData entityData){
        dataMap.put(entityData.uuid,entityData);
    }

    public  static EntityData update(LivingEntity entity){
        if(entity instanceof Player){
            PlayerData entityData= new PlayerData((Player) entity);
            dataMap.put(entity.getUniqueId(),entityData);
            return entityData;
        }
        EntityData entityData= new EntityData(entity);
        dataMap.put(entity.getUniqueId(),entityData);
        return entityData;
    }

    public  static Map<UUID, EntityData> getDataMap() {
        return dataMap;
    }

    public  static void delData(UUID uuid){
        dataMap.remove(uuid);
    }
    public static EntityData getData(LivingEntity entity){
        if(entity==null){
            return null;
        }
        EntityData entityData =dataMap.get(entity.getUniqueId());
        if(entityData==null){
            entityData=update(entity);
            Map<IAttribute,Double> sj = FAttributePlugin.plugin.getSaveEntityData(entity.getUniqueId().toString());
            if(sj!=null){
                entityData.statusMap.addModifier(new Modifier("FAttributePlugin_SaveData",FAttributePlugin.plugin,sj));
            }
        }

      return entityData;
    }
    public static PlayerData getPlayerData(Player Player){
        if(Player==null){
            return null;
        }
        EntityData entityData =dataMap.get(Player.getUniqueId());
        if(entityData==null){
            entityData=update(Player);
        }

        return (PlayerData) entityData;
    }

}
